<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsSalesInfo extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['i.purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['i.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $customers_id = $this->request->post('customers_id');
            if(!empty($customers_id)){
                $where['i.customers_id'] = $customers_id;
            }
            $pay_status = $this->request->post('pay_status');
            if($pay_status!=null && $pay_status!==''){
                $where['i.pay_status'] = $pay_status;
            }
            $examine_status = $this->request->post('examine_status');
            if($examine_status!=null && $examine_status!==''){
                $where['i.examine_status'] = $examine_status;
            }

            $field = empty(input('field'))?'goods_code':trim(input('field'));
            $order = empty(input('sort'))?'DESC':trim(input('sort'));

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_goods_sales_info')->alias('i')
                    ->join('kuyou_goods g','g.goods_code=i.goods_code')
                    ->join('kuyou_unit u','u.id=i.default_unit')
                    ->join('kuyou_customers s','s.id=i.customers_id')
                    ->join('kuyou_stores r','r.id=i.stores_id')
                    ->join('kuyou_staff_member sr','sr.id=i.sale_user')
                    ->where($where)
                    ->page($page,$limit)
                    ->order(['i.'.$field=>$order,'i.purchase_time'=>'DESC'])
                    ->field('i.id,i.goods_code,i.purchase_code,i.purchase_type,FROM_UNIXTIME(i.purchase_time,\'%Y-%m-%d\') as purchase_time,FROM_UNIXTIME(i.delivery_time,\'%Y-%m-%d\') as delivery_time
                ,i.all_money,i.number,i.tax_included,i.money_tax_price,i.unit_price,i.number as library_number,i.money_tax_price as library_money,i.purchase_money,i.tax_money,i.remark,i.purchase_order_id,
                g.goods_name,g.goods_image,g.goods_specification_id,g.goods_code,g.goods_barcode,
                s.customer_name as s_name,
                sr.name as staff_member,
                r.name as r_name,
                u.name as unitname')
                    ->select();
            }else{
                $list = Db::name('kuyou_goods_sales_info')->alias('i')
                    ->join('kuyou_goods g','g.goods_code=i.goods_code')
                    ->join('kuyou_unit u','u.id=i.default_unit')
                    ->join('kuyou_customers s','s.id=i.customers_id')
                    ->join('kuyou_stores r','r.id=i.stores_id')
                    ->join('kuyou_staff_member sr','sr.id=i.sale_user')
                    ->where($where)
                    ->order(['i.'.$field=>$order,'i.purchase_time'=>'DESC'])
                    ->field('i.id,i.goods_code,i.purchase_code,i.purchase_type,FROM_UNIXTIME(i.purchase_time,\'%Y-%m-%d\') as purchase_time,FROM_UNIXTIME(i.delivery_time,\'%Y-%m-%d\') as delivery_time
                ,i.all_money,i.number,i.tax_included,i.money_tax_price,i.unit_price,i.number as library_number,i.money_tax_price as library_money,i.purchase_money,i.tax_money,i.remark,i.purchase_order_id,
                g.goods_name,g.goods_image,g.goods_specification_id,g.goods_code,g.goods_barcode,
                s.customer_name as s_name,
                sr.name as staff_member,
                r.name as r_name,
                u.name as unitname')
                    ->select();
            }
            $row_count = Db::name('kuyou_goods_sales_info')->alias('i')
                ->join('kuyou_goods g','g.goods_code=i.goods_code')
                ->join('kuyou_unit u','u.id=i.default_unit')
                ->join('kuyou_customers s','s.id=i.customers_id')
                ->join('kuyou_stores r','r.id=i.stores_id')
                ->join('kuyou_staff_member sr','sr.id=i.sale_user')
                ->where($where)->count();

            foreach ($list as $list_k=>$list_v){
                if($list_v['purchase_type']==1){
                    $note = Db::name('kuyou_goods_sales')->where(['purchase_code'=>$list_v['purchase_code']])->value('note');
                }else{
                    $note = Db::name('kuyou_goods_sales_exchange')->where(['purchase_code'=>$list_v['purchase_code']])->value('note');
                }
                $list[$list_k]['note'] = $note;
            }

            $result = array("count" => $row_count, "list" => $list);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
